package com.mathlearning.auth.controller;

import com.mathlearning.auth.common.ApiResponse;
import com.mathlearning.auth.dto.*;
import com.mathlearning.auth.service.AuthService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    /**
     * 获取图片验证码
     */
    @GetMapping("/captcha")
    public ApiResponse<CaptchaResponse> getCaptcha(HttpServletRequest request) {
        return authService.getCaptcha(request);
    }

    /**
     * 发送短信验证码（JSON格式）
     */
    @PostMapping(value = {"/send-sms-code", "/sms/send"}, 
                 consumes = {"application/json", "application/*+json"})
    public ApiResponse<?> sendSmsCodeJson(
            @RequestBody SendSmsRequest request,
            @RequestParam(required = false) String captchaToken,
            HttpServletRequest httpRequest) {
        // 兼容：若 query 未带 captchaToken，而 body 携带 token（通过 DTO 的 captchaToken 字段）
        return authService.sendSmsCode(request, captchaToken, httpRequest);
    }
    
    /**
     * 发送短信验证码（表单格式）
     */
    @PostMapping(value = {"/send-sms-code", "/sms/send"}, 
                 consumes = {"application/x-www-form-urlencoded", "multipart/form-data"})
    public ApiResponse<?> sendSmsCodeForm(
            @RequestParam String phone,
            @RequestParam String type,
            @RequestParam(required = false) String captchaCode,
            @RequestParam(required = false) String captchaToken,
            HttpServletRequest httpRequest) {
        SendSmsRequest request = new SendSmsRequest();
        request.setPhone(phone);
        request.setType(type);
        request.setCaptchaCode(captchaCode);
        return authService.sendSmsCode(request, captchaToken, httpRequest);
    }

    /**
     * 验证码登录
     */
    @PostMapping("/login-by-sms")
    public ApiResponse<LoginResponse> loginBySms(@Valid @RequestBody SmsLoginRequest request, HttpServletRequest httpRequest) {
        return authService.loginBySms(request, httpRequest);
    }

    /**
     * 密码登录
     */
    @PostMapping({"/login-by-password", "/login/password"})  // 支持两种路径格式
    public ApiResponse<LoginResponse> loginByPassword(@Valid @RequestBody PasswordLoginRequest request, HttpServletRequest httpRequest) {
        return authService.loginByPassword(request, httpRequest);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public ApiResponse<LoginResponse> register(
            @Valid @RequestBody RegisterRequest request,
            @RequestParam(required = false) String captchaToken,
            HttpServletRequest httpRequest) {
        return authService.register(request, captchaToken, httpRequest);
    }

    /**
     * 检查手机号是否存在
     */
    @PostMapping("/check-phone")
    public ApiResponse<CheckPhoneResponse> checkPhone(@Valid @RequestBody CheckPhoneRequest request) {
        return authService.checkPhone(request);
    }

    /**
     * 获取微信登录授权URL
     */
    @GetMapping("/wx-auth-url")
    public ApiResponse<Map<String, String>> getWxAuthUrl() {
        String authUrl = authService.getWxAuthUrl();
        
        Map<String, String> result = new java.util.HashMap<>();
        result.put("authUrl", authUrl);
        
        return ApiResponse.success(result);
    }

    /**
     * 微信登录回调
     */
    @GetMapping("/wx-callback")
    public ApiResponse<LoginResponse> wxCallback(@RequestParam String code,
                                               @RequestParam(required = false) String state,
                                               HttpServletRequest request) {
        return authService.loginByWeChat(code, request);
    }
}



